import { i18nRender } from '@/locales'

export const PERMISSION_ENUM = {
  'add': { key: 'add', label: i18nRender('all.autotext409') },
  'delete': { key: 'delete', label: i18nRender('all.autotext379') },
  'edit': { key: 'edit', label: i18nRender('all.autotext904') },
  'query': { key: 'query', label: i18nRender('all.autotext30') },
  'get': { key: 'get', label: i18nRender('all.autotext822') },
  'enable': { key: 'enable', label: i18nRender('all.autotext307') },
  'disable': { key: 'disable', label: i18nRender('all.autotext567') },
  'import': { key: 'import', label: i18nRender('all.autotext990') },
  'export': { key: 'export', label: i18nRender('all.autotext1014') }
}

/**
 * <a-button v-if="$auth('form.edit')">Button</a-button>
 * @param Vue
 */
function plugin (Vue) {
  if (plugin.installed) {
    return
  }

  !Vue.prototype.$auth && Object.defineProperties(Vue.prototype, {
    $auth: {
      get () {
        const _this = this
        return (permissions) => {
          const action = permissions.split('.')[1]
          // 获取按钮权限
          const permissionList = _this.$store.getters.pms
          if (permissionList == null || permissionList.length === 0) {
            return false
          }
          return permissionList.findIndex((val) => {
            return val === action
          }) > -1
        }
      }
    }
  })

  !Vue.prototype.$isSystem && Object.defineProperties(Vue.prototype, {
    $isSystem: {
      get () {
        const _this = this
        return () => {
          const info = _this.$store.getters.userInfo
          return !(info && info.isAdmin === 1)
        }
      }
    }
  })

  !Vue.prototype.$enum && Object.defineProperties(Vue.prototype, {
    $enum: {
      get () {
        // const _this = this;
        return (val) => {
          let result = PERMISSION_ENUM
          val && val.split('.').forEach(v => {
            result = result && result[v] || null
          })
          return result
        }
      }
    }
  })

  !Vue.prototype.$menuAuth && Object.defineProperties(Vue.prototype, {
    $menuAuth: {
      get () {
        const _this = this
        return (permission) => {
          // 获取按钮权限
          const permissionList = _this.$store.getters.menus
          if (permissionList == null || permissionList.length === 0) {
            return false
          }
          return permissionList.findIndex((val) => {
            return val === permission
          }) > -1
        }
      }
    }
  })
}

export default plugin
